package com.jackrain.nea.ad.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.ad.api.AdmonitorQueryCmd;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySessionImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(value = "AD_MONIOTR",description = "监控器设置")
@RestController
public class AdmonitorCtrl {
//    @Reference(version = "1.0", group = "ad")
//    private AdmonitorSaveCmd admonitorSaveCmd;
//    @Reference(version = "1.0", group = "ad")
//    private AdmonitorQueryCmd admonitorQueryCmd;
//    @Reference(version = "1.0", group = "ad")
//    private AdmonitorDelCmd admonitorDelCmd;
//
//    @ApiOperation(value = "监控器设置保存")
//    @RequestMapping(path = "/p/cs/admonitorsave",method = RequestMethod.GET)
//    public JSONObject testSave(HttpServletRequest request,
//                                  @RequestParam(value="param",required = true) String param) throws Exception {
////        User user = (User) Security4Utils.getUser("root");
////        QuerySessionImpl querySession = new QuerySessionImpl(user);
//        ValueHolder valueHolder = new ValueHolder();
//        QuerySessionImpl querySession = new QuerySessionImpl(request);
//        DefaultWebEvent event = new DefaultWebEvent("test",request,false);
//        event.put("param", JSON.parseObject(param));
//        querySession.setEvent(event);
//        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.ad.api.AdmonitorSaveCmd", "ad", "1.0");
//        ValueHolder result = (ValueHolder)((AdmonitorSaveCmd)o).execute(querySession);
//        return result.toJSONObject();
//    }
//
//    @ApiOperation(value = "监控器查询")
//    @RequestMapping(path = "/p/cs/admonitorquery",method = RequestMethod.GET)
//    public JSONObject testQuery(HttpServletRequest request,
//                              @RequestParam(value="param",required = true) String param) throws Exception {
//        ValueHolder valueHolder = new ValueHolder();
//        QuerySessionImpl querySession = new QuerySessionImpl(request);
//        DefaultWebEvent event = new DefaultWebEvent("test", request, false);
//        event.put("param", JSON.parseObject(param));
//        querySession.setEvent(event);
//        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.ad.api.AdmonitorQueryCmd", "ad", "1.0");
//        ValueHolder result = (ValueHolder) ((AdmonitorQueryCmd) o).execute(querySession);
//        return result.toJSONObject();
//    }
//
//    @ApiOperation(value = "监控器删除")
//    @RequestMapping(path = "/p/cs/admonitordel",method = RequestMethod.GET)
//    public JSONObject testDel(HttpServletRequest request,
//                              @RequestParam(value="param",required = true) String param) throws Exception {
//        ValueHolder valueHolder = new ValueHolder();
//        QuerySessionImpl querySession = new QuerySessionImpl(request);
//        DefaultWebEvent event = new DefaultWebEvent("test", request, false);
//        event.put("param", JSON.parseObject(param));
//        querySession.setEvent(event);
//        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.ad.api.AdmonitorDelCmd", "ad", "1.0");
//        ValueHolder result = (ValueHolder) ((AdmonitorDelCmd) o).execute(querySession);
//        return result.toJSONObject();
//    }
//
//    @ApiOperation(value = "监控器重载")
//    @RequestMapping(path = "/p/cs/admonitorload",method = RequestMethod.GET)
//    public JSONObject testLoad(HttpServletRequest request,
//                              @RequestParam(value="param",required = false) String param) throws Exception {
//        ValueHolder valueHolder = new ValueHolder();
//        QuerySessionImpl querySession = new QuerySessionImpl(request);
//        DefaultWebEvent event = new DefaultWebEvent("test", request, false);
//        event.put("param", JSON.parseObject(param));
//        querySession.setEvent(event);
//        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.ad.api.AdmonitorLoadCmd", "ad", "1.0");
//        ValueHolder result = (ValueHolder) ((AdmonitorLoadCmd) o).execute(querySession);
//        return result.toJSONObject();
//    }

    @ApiOperation(value = "跨库监控器查询")
    @RequestMapping(path = "/fc/api/admonitorquery",method ={RequestMethod.GET, RequestMethod.POST})
    public JSONObject apiQuery(HttpServletRequest request,
                                @RequestParam(value="param",required = true) String param) throws Exception {
        ValueHolder valueHolder = new ValueHolder();
        QuerySessionImpl querySession = new QuerySessionImpl(request);
        DefaultWebEvent event = new DefaultWebEvent("test", request, false);
        event.put("param", JSON.parseObject(param));
        querySession.setEvent(event);
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.ad.api.AdmonitorQueryCmd", "ad", "1.0");
        ValueHolder result = (ValueHolder) ((AdmonitorQueryCmd) o).execute(querySession);
        return result.toJSONObject();
    }
}
